package com.kvsl.wechatmonitor.service;

import com.kvsl.wechatmonitor.domain.Message;
import com.kvsl.wechatmonitor.repository.MessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Objects;

@Service
public class MessageService {

    private MessageRepository messageRepository;

    @Autowired
    public MessageService(MessageRepository messageRepository) {
        this.messageRepository = messageRepository;
    }

    public List<Message> searchMessage(String sender) {
        String from = Objects.requireNonNull(sender, "sender不能为空");
        return messageRepository.getMessageBySender(from);
    }

    public List<Message> searchProidMessage(String time) {
        LocalDateTime recent = LocalDateTime.parse(time);
        return messageRepository.getRecentMessage(recent);
    }

    public List<Message> searchRecentMessage() {
        LocalDateTime yestoday = LocalDateTime.now().minusDays(1);
        return messageRepository.getRecentMessage(yestoday);
    }
}
